/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package us.oh.k12.wkw.robot.subsystem;

import edu.wpi.first.wpilibj.command.Subsystem;
import edu.wpi.first.wpilibj.networktables.NetworkTable;
import java.util.Date;
import us.oh.k12.wkw.robot.util.SimpleDateFormat;

/**
 * Base class for all systems.
 */
public abstract class SystemBase extends Subsystem {

    protected SystemBase() {
        super();
    }

    /**
     * @param name
     */
    public SystemBase(final String pName) {
        super(pName);
    }

    /*
     protected void dumpJaguar(final int pModule, final Jaguar pJaguar) {
     if (null == pJaguar) {
     this.debug("dumpJaguar()", pModule + " is not present.");
     } else {
     this.debug("dumpJaguar()", "Channel=" + pJaguar.getChannel() + ".");
     this.debug("dumpJaguar()", "ModuleNumber=" + pJaguar.getModuleNumber() + ".");
     this.debug("dumpJaguar()", "Position=" + pJaguar.getPosition() + ".");
     this.debug("dumpJaguar()", "Description=" + pJaguar.getDescription() + ".");
     this.debug("dumpJaguar()", "Expiration=" + pJaguar.getExpiration() + ".");
     this.debug("dumpJaguar()", "get=" + pJaguar.get() + ".");
     this.debug("dumpJaguar()", "Raw=" + pJaguar.getRaw() + ".");
     this.debug("dumpJaguar()", "Speed=" + pJaguar.getSpeed() + ".");
     this.debug("dumpJaguar()", "isAlive=" + pJaguar.isAlive() + ".");
     this.debug("dumpJaguar()", "isSafetyEnabled=" + pJaguar.isSafetyEnabled() + ".");
     }
     }

     protected void findChannels() {
     Jaguar aJaguar = null;

     this.debug("findChannels()", "Started.");

     for (int idx = 1; idx < 11; idx++) {

     try {

     this.debug("findChannels()", "Trying channel=" + idx + ".");
     aJaguar = new Jaguar(idx);
     this.dumpJaguar(idx, aJaguar);
     aJaguar.set(1.0);
     this.debug("findChannels()", "channel[" + idx + "] get=" + aJaguar.get() + ".");
     Timer.delay(0.25);
     aJaguar.set(0.0);
     aJaguar.free();

     } catch (AllocationException anAloEx) {
     this.error("findChannels()", "AllocationException.", null);
     }

     }

     this.debug("findChannels()", "Ended.");
     }
     */
    /*
     * 
     * support methods.
     * 
     */
    protected void debug(final String pMethod, final String pMessage) {
        //WkwFrcLogger.debug(this.getClassName(), pMethod, pMessage);

        NetworkTable.getTable("WkwFrcLogger").putString(
                "log", this.formatMessage(new Date(), this.getClassName(), "D", pMethod, pMessage));

        System.out.println(pMessage);
    }

    protected String formatMessage(final Date pMessageDate, final String pClazz,
            final String pLevel, final String pMethod, final String pMessage) {

        final StringBuffer aMsg = new StringBuffer();

        aMsg.append(SystemBase.formatDate(pMessageDate));
        aMsg.append(SystemBase.SEPERATOR);
        aMsg.append(pLevel);
        aMsg.append(SystemBase.SEPERATOR);
        aMsg.append(pClazz);
        aMsg.append(SystemBase.SEPERATOR);
        aMsg.append(pMethod);
        aMsg.append(SystemBase.SEPERATOR);
        aMsg.append(pMessage);

        return aMsg.toString();
    }

    protected static String formatDate(final Date pDate) {
        final SimpleDateFormat aFormat = new SimpleDateFormat(SystemBase.FORMAT);
        return (null == pDate ? "" : aFormat.format(pDate));
    }
    private static final char SEPERATOR = ',';
    private static final String FORMAT = "yyyy/mm/dd_HH:mm:ss.sss";

    protected void info(final String pMethod, final String pMessage) {
        //WkwFrcLogger.info(this.getClassName(), pMethod, pMessage);
        NetworkTable.getTable("WkwFrcLogger").putString(
                "log", this.formatMessage(new Date(), this.getClassName(), "I", pMethod, pMessage));

        System.out.println(pMessage);
    }

    /*
     protected void reportSelfTestState(final String pSystem, final String pSensor,
     final String pState, final boolean pPassed, final String pMessage) {

     if (pPassed) {

     WkwFrcLogger.info(this.getClassName(), "reportSelfTestState()", "System:" + pSystem
     + ", Sensor:" + pSensor + ", State:" + pState + ", Passed:" + pPassed
     + ", Message:" + pMessage + ".");

     } else {

     WkwFrcLogger.error(this.getClassName(), "reportSelfTestState()", "System:" + pSystem
     + ", Sensor:" + pSensor + ", State:" + pState + ", Passed:" + pPassed
     + ", Message:" + pMessage + ".", null);

     }

     }
     */
    private String formatException(final Exception pEx) {
        return (null == pEx ? "." : pEx.getClass().getName() + ", message=" + pEx.getMessage()
                + ".");
    }

    protected void error(final String pMethod, final Exception pEx) {
        this.error(pMethod, this.formatException(pEx), pEx);
    }

    protected void error(final String pMethod, final String pMessage, final Exception pEx) {
        //WkwFrcLogger.error(this.getClassName(), pMethod, pMessage, pEx);
        //if (null != pEx) {
        //    pEx.printStackTrace();
        //}
        NetworkTable.getTable("WkwFrcLogger").putString(
                "log", this.formatMessage(new Date(), this.getClassName(), "E", pMethod, pMessage));

        System.out.println(pMessage);
        if (null != pEx) {
            pEx.printStackTrace();
        }
    }

    protected String getDateTimeFormatted() {
        final SimpleDateFormat aformat = new SimpleDateFormat();
        return aformat.format(new Date());
    }

    protected String getClassName() {
        String aClassName = this.getClass().getName();
        return aClassName.substring(aClassName.lastIndexOf('.') + 1);
    }
}
